package com.yonxao.demo.crud.annotation.serializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;

/**
 * 反序列化换转换器 <br>
 * 将浮点数乘以 100 后强转为 Integer
 *
 * @author xiaosaguo
 * @date 2020/08/17 11:53
 */
public class FloatMultiplyHundredToIntegerJsonDeserializer extends JsonDeserializer<Object> {

    @Override
    public Object deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        Number value = jsonParser.getNumberValue();
        if (value != null) {
            // 支持小数
            return (int) (value.doubleValue() * 100);
        }
        return null;
    }
}